﻿# FindDuckX
# --------
#
# Find the DuckX libraries(Only for DuckX provided by FastCAE)
#
# Result Variables
# ^^^^^^^^^^^^^^^^
#
# The following variables will be defined:
#
# ``DuckX_FOUND`` True if DuckX found on the local system
#
# ``DuckX_VERSION`` DuckX Version(x.x.x)
#
# ``DuckX_VERSION_MAJOR`` DuckX Major Version
#
# ``DuckX_VERSION_MINOR`` DuckX Minor Version
#
# ``DuckX_VERSION_PATCH`` DuckX Patch Version
#
# ``DuckX_DIRS`` Location of DuckX(root dir)
#
# ``DuckX_INCLUDE_DIRS`` Location of DuckX header files
#
# ``DuckX_LIBRARY_DIRS`` Location of DuckX libraries
#
# ``DuckX_LIBRARIES`` List of the DuckX libraries found
#

# 防止重复引入
if(FASTCAE_DUCKX_ALREADY_INCLUDED)
	return()
endif()
set(FASTCAE_DUCKX_ALREADY_INCLUDED 1)

# find_path 搜索包含某个文件的路径
# 如果在某个路径下发现了该文件，该结果会被存储到该变量中；如果没有找到，存储的结果将会是<VAR>-NOTFOUND
find_path(DuckX_DIRS 
	NAMES 
		include/duckx.hpp
	PATHS 
		${CMAKE_SOURCE_DIR}/extlib/DuckX 
	NO_SYSTEM_ENVIRONMENT_PATH
	NO_CMAKE_SYSTEM_PATH
)

set(DuckX_VERSION_MAJOR 1)
set(DuckX_VERSION_MINOR 2)
set(DuckX_VERSION_PATCH 2)

set(DuckX_VERSION "${DuckX_VERSION_MAJOR}.${DuckX_VERSION_MINOR}.${DuckX_VERSION_PATCH}")

find_path(DuckX_INCLUDE_DIRS
	NAMES
		duckx.hpp
	HINTS
		${DuckX_DIRS}/include
)

find_path(DuckX_LIBRARY_DIRS
	NAMES
		duckx.lib libduckx.so
	HINTS
		${DuckX_DIRS}/lib
)

set(DuckX_LIBRARIES FASTCAE::DUCKX)

add_library(FASTCAE::DUCKX SHARED IMPORTED)
set_property(TARGET FASTCAE::DUCKX PROPERTY INTERFACE_INCLUDE_DIRECTORIES ${DuckX_INCLUDE_DIRS})
set_property(TARGET FASTCAE::DUCKX APPEND PROPERTY IMPORTED_CONFIGURATIONS RELEASE)

if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
	set_target_properties(FASTCAE::DUCKX PROPERTIES
		IMPORTED_LOCATION_RELEASE "${DuckX_LIBRARY_DIRS}/libduckx.so"
		#IMPORTED_SONAME_RELEASE "${DuckX_LIBRARY_DIRS}/libDuckX.so"
	)
elseif(CMAKE_SYSTEM_NAME STREQUAL "Windows")
	set_target_properties(FASTCAE::DUCKX PROPERTIES
		IMPORTED_IMPLIB_RELEASE "${DuckX_LIBRARY_DIRS}/duckx.lib"
		IMPORTED_LOCATION_RELEASE "${DuckX_DIRS}/bin/duckx.dll"
	)
endif()

include(FindPackageHandleStandardArgs)

# 如果找到所有需要的变量，并且版本匹配，则将DuckX_FOUND变量设置为TRUE
find_package_handle_standard_args(DuckX
	FOUND_VAR
		DuckX_FOUND
	REQUIRED_VARS
		DuckX_DIRS
		DuckX_INCLUDE_DIRS
		DuckX_LIBRARY_DIRS
		DuckX_LIBRARIES
	VERSION_VAR
		DuckX_VERSION
)